package korwin;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

/**
 * Enemy helicopter.
 * 
 * @author www.gametutorial.net
 */

public class Gadget {
    
    // For creating new enemies.
    private static final long timeBetweenNewEnemiesInit = Framework.secInNanosec * 3;
    public static long timeBetweenNewEnemies = timeBetweenNewEnemiesInit;
    public static long timeOfLastCreatedEnemy = 0;
    
    // Position of the helicopter on the screen.
    public double xCoordinate;
    public double yCoordinate;
    
    // Moving speed and direction.
    private final double movingXspeedInit = -3.5;
    private double movingXspeed = movingXspeedInit;
    
    // Images of enemy helicopter. Images are loaded and set in Game class in LoadContent() method.
    public BufferedImage gadgetImg;
   // public static BufferedImage helicopterFrontPropellerAnimImg;
    //public static BufferedImage helicopterRearPropellerAnimImg;
    
    // Animation of the helicopter propeller.
    //private Animation helicopterFrontPropellerAnim;
    //private Animation helicopterRearPropellerAnim;
    // Offset for the propeler. We add offset to the position of the position of helicopter.
    //private static int offsetXFrontPropeller = -35;
    //private static int offsetYFrontPropeller = -30;
    //private static int offsetXRearPropeller = 205;
    //private static int offsetYRearPropeller = 6;


    /**
     * Initialize enemy helicopter.
     * 
     * @param xCoordinate Starting x coordinate of helicopter.
     * @param yCoordinate Starting y coordinate of helicopter.
     * @param gadgetImg Image of helicopter body.
     * @param helicopterFrontPropellerAnimImg Image of front helicopter propeller.
     * @param helicopterRearPropellerAnimImg Image of rear helicopter propeller.
     */
    public void Initialize(int xCoordinate, int yCoordinate, double movingXspeed, URL gadgetImgUrl)
    {
    	try {
			gadgetImg = ImageIO.read(gadgetImgUrl);
    	}
    	catch (IOException ex) {
            System.out.println(ex);
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
    	}
    	
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        
        // Initialize animation object.
        //helicopterFrontPropellerAnim = new Animation(helicopterFrontPropellerAnimImg, 75, 99, 8, 40, true, xCoordinate + offsetXFrontPropeller, yCoordinate + offsetYFrontPropeller, 0);
        //helicopterRearPropellerAnim = new Animation(helicopterRearPropellerAnimImg, 47, 47, 10, 10, true, xCoordinate + offsetXRearPropeller, yCoordinate + offsetYRearPropeller, 0);
       
        // Moving speed and direction of enemy.
        this.movingXspeed = movingXspeed;
        Gadget.timeBetweenNewEnemies = Framework.secInNanosec * ((int)(Math.random() * 15) + 5);
    }
    
    /**
     * It sets speed and time between enemies to the initial properties.
     */
    public static void restartEnemy(){
        Gadget.timeBetweenNewEnemies = timeBetweenNewEnemiesInit;
        Gadget.timeOfLastCreatedEnemy = 0;
    }
    
    /**
     * Checks if the enemy is left the screen.
     * 
     * @return true if the enemy is left the screen, false otherwise.
     */
    public boolean isLeftScreen()
    {
        if(xCoordinate < 0 - gadgetImg.getWidth()) // When the entire helicopter is out of the screen.
            return true;
        else
            return false;
    }
    
        
    /**
     * Updates position of helicopter, animations.
     */
    public void Update()
    {
        // Move enemy on x coordinate.
        xCoordinate += movingXspeed;
        
        // Moves helicoper propeler animations with helicopter.
       // helicopterFrontPropellerAnim.changeCoordinates(xCoordinate + offsetXFrontPropeller, yCoordinate + offsetYFrontPropeller);
        //helicopterRearPropellerAnim.changeCoordinates(xCoordinate + offsetXRearPropeller, yCoordinate + offsetYRearPropeller);
    }
    
    
    /**
     * Draws helicopter to the screen.
     * 
     * @param g2d Graphics2D
     */
    public void Draw(Graphics2D g2d)
    { 
        g2d.drawImage(gadgetImg, (int)xCoordinate, (int)yCoordinate, null);
    }
    
}
